import * as R from 'ramda';
import * as changeCase from 'change-case';

import { MenuType } from '../../../../../lib/enums';

const toPartCode = (name) =>
  R.compose(
    R.applySpec({
      enums: R.reduce((acc, item) => {
        const res = `\t\t'${item.code}',\n`;
        return acc + res;
      }, ''),
      options: R.reduce((acc, item) => {
        const res = `\t\t{ label: '${
          item.name
        }', value: ${changeCase.capitalCase(name)}Enum.${item.code} },\n`;
        return acc + res;
      }, ``),
    }),
  );
const toEnumCode = R.curry((name, value) => {
  const variableName = `${changeCase.capitalCase(name)}Enum`;
  return `export const ${variableName} = OptionUtils.createEnum([\n${value}])\n`;
});
const toOptionCode = R.curry((name, value) => {
  const variableName = `${changeCase.capitalCase(name)}Options`;
  return `export const ${variableName} = OptionUtils.createOptions([\n${value}])\n`;
});

const genCode = (name) =>
  R.compose(
    (data) =>
      `${toEnumCode(name, data.enums)}\n${toOptionCode(name, data.options)}`,
    toPartCode(name),
  );

const genMenuCode = R.compose(
  genCode('menu'),
  R.filter(R.compose(MenuType.isMenu, R.prop('type'))),
);
const genButtonCode = R.compose(
  genCode('button'),
  R.filter(R.compose(MenuType.isButton, R.prop('type'))),
);

export const toMagicianEnumCode = (data) => {
  return `
import { OptionUtils } from '@r-magician/utils';
${genMenuCode(data)}
${genButtonCode(data)}
  `;
};
